package org.ala.tiktools.sharddb.mybatis.pager.parameter;

import org.ala.tiktools.mybatis.interfaces.AbstractOrderBy;
import org.ala.tiktools.mybatis.pager.Pager;
import org.ala.tiktools.sharddb.mybatis.interceptor.Shard;
import org.ala.tiktools.sharddb.mybatis.pager.with_order.ShardCountDO;
import org.ala.tiktools.sharddb.mybatis.router.ShardInfo;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 本轮分页查询的执行参数
 *
 * @author ala
 * @date 2025-03-05 12:43
 */
public class ShardPagerParameter {
    /**
     *  分库分表注解
     */
    protected Shard anno;
    /**
     *  待执行的线程池
     */
    protected ThreadPoolExecutor threadPool;
    /**
     *  分页对象在方法参数中的下标
     */
    protected int pagerIdx;
    /**
     *  分页对象
     */
    protected Pager pager;
    /**
     *  本轮查询的数据总数，原始的no, size
     */
    protected int total = -1, pageNo = -1, pageSize = -1;
    /**
     *  当前总量和分量
     */
    protected ShardCountDO currentCountDO;
    /**
     *  本次分页在数据范围内的[left, right]
     */
    protected int left, right;
    /**
     *  排序对象在方法中的下标
     */
    protected Integer orderByIdx;
    /**
     *  排序对象
     */
    protected AbstractOrderBy orderBy;
    /**
     *  当前用的第几个排序字段
     *  <p> 从0开始
     */
    protected int orderIndex;
    /**
     *  执行方法的对象
     */
    protected Object proxy;
    /**
     *  执行的查数量的方法
     */
    protected CountMethod countMethod;
    /**
     *  执行的查数据的方法
     */
    protected DataMethod dataMethod;

    public static ShardPagerParameter build(
            Shard anno, ThreadPoolExecutor threadPool,
            int pagerIdx, Pager pager,
            Integer orderByIdx, AbstractOrderBy orderBy,
            int orderIndex,
            CountMethod countMethod, DataMethod dataMethod) {
        ShardPagerParameter o = new ShardPagerParameter();
        o.anno = anno;
        o.threadPool = threadPool;
        o.pagerIdx = pagerIdx;
        o.pager = pager.queryTotal(false);
        o.pageNo = pager.getNo();
        o.pageSize = pager.getSize();
        o.left = pager.getOffset() + 1; o.right = o.left + pager.getSize() - 1;
        o.orderByIdx = orderByIdx;
        o.orderBy = orderBy;
        o.orderIndex = orderIndex;
        o.countMethod = countMethod;
        o.dataMethod = dataMethod;
        return o;
    }

    public int getPagerIdx() {
        return pagerIdx;
    }

    public void setPagerIdx(int pagerIdx) {
        this.pagerIdx = pagerIdx;
    }

    public Pager getPager() {
        return pager;
    }

    public void setPager(Pager pager) {
        this.pager = pager;
    }

    public Integer getOrderByIdx() {
        return orderByIdx;
    }

    public void setOrderByIdx(Integer orderByIdx) {
        this.orderByIdx = orderByIdx;
    }

    public AbstractOrderBy getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(AbstractOrderBy orderBy) {
        this.orderBy = orderBy;
    }

    public int getOrderIndex() {
        return orderIndex;
    }

    public void setOrderIndex(int orderIndex) {
        this.orderIndex = orderIndex;
    }

    public Object getProxy() {
        return proxy;
    }

    public void setProxy(Object proxy) {
        this.proxy = proxy;
    }

    public Shard getAnno() {
        return anno;
    }

    public void setAnno(Shard anno) {
        this.anno = anno;
    }

    public ThreadPoolExecutor getThreadPool() {
        return threadPool;
    }

    public void setThreadPool(ThreadPoolExecutor threadPool) {
        this.threadPool = threadPool;
    }

    public int getLeft() {
        return left;
    }

    public void setLeft(int left) {
        this.left = left;
    }

    public int getRight() {
        return right;
    }

    public void setRight(int right) {
        this.right = right;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public ShardPagerParameter total(int total) {
        setTotal(total);
        return this;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public CountMethod getCountMethod() {
        return countMethod;
    }

    public void setCountMethod(CountMethod countMethod) {
        this.countMethod = countMethod;
    }

    public DataMethod getDataMethod() {
        return dataMethod;
    }

    public void setDataMethod(DataMethod dataMethod) {
        this.dataMethod = dataMethod;
    }

    public ShardCountDO getCurrentCountDO() {
        return currentCountDO;
    }

    public void setCurrentCountDO(ShardCountDO currentCountDO) {
        this.currentCountDO = currentCountDO;
    }

    public ShardPagerParameter currentCountDO(ShardCountDO currentCountDO) {
        setCurrentCountDO(currentCountDO);
        return this;
    }
}
